package com.jg.utils.reflex.classes;

import com.jg.utils.reflex.en.User;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;

public class FieldTest {

    @Test
    public void testFieldGet()throws Exception{
        Class<User> userClass = User.class;
        User user = new User("张氏",23,"233");
        System.out.println("反射前"+user);


        Field field = userClass.getDeclaredField("name");
        //暴力反射，忽悠修饰符
        field.setAccessible(true);
        //获取知道对象当前字段的值
        System.out.println(field.get(user));

    }


    @Test
    public void testFieldSet()throws Exception{
        Class<User> userClass = User.class;
        User user = new User("张氏",23,"233");
        System.out.println("反射前"+user);


        Field field = userClass.getDeclaredField("name");
        //暴力反射，忽悠修饰符
        field.setAccessible(true);
        field.set(user,"李四");
        //获取知道对象当前字段的值
        System.out.println("反射后"+user);

    }


}
